package com.ruoyi.project.system.course.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.courseClass.domain.CourseClass;
import com.ruoyi.project.system.courseClass.service.ICourseClassService;
import com.ruoyi.project.system.grades.domain.ClassGrades;
import com.ruoyi.project.system.grades.service.IClassGradesService;
import com.ruoyi.project.system.student.domain.ClassStudent;
import com.ruoyi.project.system.student.service.IClassStudentService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.mapper.UserMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.course.domain.Course;
import com.ruoyi.project.system.course.service.ICourseService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 课程Controller
 *
 * @author ruoyi
 * @date 2021-04-07
 */
@Controller
@RequestMapping("/system/course")
public class CourseController extends BaseController
{
    private String prefix = "system/course";

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ICourseService courseService;

    @Autowired
    private IClassGradesService classGradesService;

    @Autowired
    private ICourseClassService courseClassService;

    @Autowired
    private IClassStudentService classStudentService;

    @RequiresPermissions("system:course:view")
    @GetMapping()
    public String course()
    {
        return prefix + "/course";
    }

    /**
     * 查询课程列表
     */
    @RequiresPermissions("system:course:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Course course)
    {
        startPage();
        User user = ShiroUtils.getSysUser();
        if(user.isTeacher()){
            course.setTeacherId(user.getUserId());
        }
        List<Course> list = courseService.selectCourseList(course);
        return getDataTable(list);
    }

    /**
     * 导出课程列表
     */
    @RequiresPermissions("system:course:export")
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Course course)
    {
        List<Course> list = courseService.selectCourseList(course);
        ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
        return util.exportExcel(list, "course");
    }

    /**
     * 新增课程
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        // 获取所有教师信息,教师角色为3
        List<User> teacherList = userMapper.selectUserListByRoleId(3L);
        mmap.put("teacherList",teacherList);
        return prefix + "/add";
    }

    /**
     * 新增保存课程
     */
    @RequiresPermissions("system:course:add")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Course course)
    {
        return toAjax(courseService.insertCourse(course));
    }

    /**
     * 修改课程
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Course course = courseService.selectCourseById(id);
        mmap.put("course", course);
        // 获取所有教师信息,教师角色为3
        List<User> teacherList = userMapper.selectUserListByRoleId(3L);
        mmap.put("teacherList",teacherList);
        return prefix + "/edit";
    }

    /**
     * 修改保存课程
     */
    @RequiresPermissions("system:course:edit")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Course course)
    {
        return toAjax(courseService.updateCourse(course));
    }

    /**
     * 删除课程
     */
    @RequiresPermissions("system:course:remove")
    @Log(title = "课程", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(courseService.deleteCourseByIds(ids));
    }



    /**
     * 管理班级
     */
    @GetMapping("/manageGrade/{id}")
    public String manageGrade(@PathVariable("id") Long id, ModelMap mmap)
    {
        Course course = courseService.selectCourseById(id);
        mmap.put("course", course);
        CourseClass condition = new CourseClass();
        condition.setCourseId(id);
        List<CourseClass> courseClassList = courseClassService.selectCourseClassList(condition);
        StringBuilder stringBuilder = new StringBuilder();
        if(courseClassList.size() > 0){
            for (int i = 0; i < courseClassList.size() ; i++) {
                stringBuilder.append(courseClassList.get(i).getClassId());
                if(i == courseClassList.size()-1){
                    break;
                }
                stringBuilder.append(",");
            }
        }
        course.setClassIds(stringBuilder.toString());
        // 获取所有班级信息
        List<ClassGrades> classGradesList = classGradesService.selectClassGradesList(new ClassGrades());
        mmap.put("classGradesList",classGradesList);
        // 获取所有教师信息,教师角色为3
        List<User> teacherList = userMapper.selectUserListByRoleId(3L);
        mmap.put("teacherList",teacherList);
        return prefix + "/manageGrade";
    }

    /**
     * 修改保存课程
     */
//    @RequiresPermissions("system:course:edit")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PostMapping("/manageGrade")
    @ResponseBody
    public AjaxResult manageGrade(Course course)
    {
        return toAjax(courseService.manageGrade(course));
    }


    /**
     * 管理学生
     */
    @GetMapping("/manageStudent/{id}")
    public String manageStudent(@PathVariable("id") Long id, ModelMap mmap)
    {
        ClassGrades classGrades = classGradesService.selectClassGradesById(id);
        mmap.put("classGrades", classGrades);
        ClassStudent condition = new ClassStudent();
        condition.setClassId(id);
        List<ClassStudent> classStudentList = classStudentService.selectClassStudentList(condition);
        StringBuilder stringBuilder = new StringBuilder();
        if(classStudentList.size() > 0){
            for (int i = 0; i < classStudentList.size() ; i++) {
                stringBuilder.append(classStudentList.get(i).getStudentId());
                if(i == classStudentList.size()-1){
                    break;
                }
                stringBuilder.append(",");
            }
        }
        classGrades.setStudentIds(stringBuilder.toString());
        // 获取所有学生信息,教师角色为4
        List<User> studentList = userMapper.selectUserListByRoleId(4L);
        mmap.put("studentList",studentList);
        return  "system/grades/manageStudent";
    }

    /**
     * 修改保存课程
     */
//    @RequiresPermissions("system:course:edit")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PostMapping("/manageStudent")
    @ResponseBody
    public AjaxResult manageStudent(ClassGrades classGrades)
    {
        return toAjax(courseService.manageStudent(classGrades));
    }


}
